(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("localeService", localService);
	localService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory' ];
	function localService($q, loggerFactory, commonService, ajaxServiceFactory) {
		var log = loggerFactory.getLogger("localeService");
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getSearchLocaleDatas :{
				url : 'do/cda/dataList/searchLocaleDatas',
				method : 'get'
			},
			getLocaleDatas : {
				url : 'do/cda/dataList/localeDatas',
				method : 'get'
			},
			getLocale : {
				url : 'do/cda/locale/localeFill',
				method : 'post'
			}
		});

		function getLocaleDatas(){
			var defer = $q.defer();
			ajaxService.getLocaleDatas().then(function(response) {
				var datas = {};
				if(response && response.data){
					// get countryCodeOptions
	                var options1 = [];
					angular.forEach(response.data.countryCodes, function(data, index, array){
						options1.push({"value" : data.value, "label" : data.label});
					});
					datas.countryCodeOptions = options1;
					
					// get localeOptions
					var options2 = [];
					angular.forEach(response.data.locales, function(data, index, array){
						options2.push({"value" : data.value, "label" : data.label});
					});
					datas.localeOptions = options2;
					
					// get languageCodeOptions
					var options3 = [];
					angular.forEach(response.data.languages, function(data, index, array){
						options3.push({"value" : data.value, "label" : data.label});
					});
					datas.languageCodeOptions = options3;
				}
				defer.resolve(datas);
			});
			return defer.promise;
		}
		
		function getSearchLocaleDatas(){
			var defer = $q.defer();
			ajaxService.getSearchLocaleDatas().then(function(response) {
				var datas = {};
				if(response && response.data){
					// get yesNoOptions
	                var options1 = {};
	                options1.data = response.data.yesNoOptions;
					datas.yesNoOptions = options1;
					
					// get countryCodeOptions
					var options2 = {};
					options2.data = response.data.countryCodeOptions;
					datas.countryCodeOptions = options2;
					
					// get dataStatusOptions
					var options3 = {};
					options3 = response.data.dataStatusOptions;
					datas.dataStatusOptions = options3;
				}
				defer.resolve(datas);
			});
			return defer.promise;
		}
		
		 function setLocale(locale){
			 var params = {
						localeId : locale.customRefLocale
		     };
			 if(locale.getDataWay=="B"){
				 locale.getDataWay="F";
			 }else{
				if(params.localeId !=null && params.localeId !=""){
					ajaxService.getLocale(params).then(function(response) {
							if(response && response.data){
								locale.decSeparator = response.data.decSeparator;
								locale.grpSeparator = response.data.grpSeparator;
								locale.dateSeparator = response.data.dateSeparator;
								locale.timeSeparator = response.data.timeSeparator;
								locale.fmtLongtimestampz = response.data.fmtLongtimestampz;
								locale.fmtTimestampz = response.data.fmtTimestampz;
								locale.fmtLongtimestamp = response.data.fmtLongtimestamp;
								locale.fmtTimestamp = response.data.fmtTimestamp;
								locale.fmtDate = response.data.fmtDate;
								locale.fmtLongtime = response.data.fmtLongtime;
								locale.fmtTime = response.data.fmtTime;
								locale.fmtShorttime = response.data.fmtShorttime;
								locale.fmtMoney = response.data.fmtMoney;
								locale.fmtInteger = response.data.fmtInteger;
								locale.fmtFloat = response.data.fmtFloat;
								locale.fmtMonth = response.data.fmtMonth;
								locale.fmtYear = response.data.fmtYear;
							}
						}, angular.noop);
				}else{
					locale.decSeparator = null;
					locale.grpSeparator = null;
					locale.dateSeparator = null;
					locale.timeSeparator = null;
					locale.fmtLongtimestampz = null;
					locale.fmtTimestampz = null;
					locale.fmtLongtimestamp = null;
					locale.fmtTimestamp = null;
					locale.fmtDate = null;
					locale.fmtLongtime = null;
					locale.fmtTime = null;
					locale.fmtShorttime = null;
					locale.fmtMoney = null;
					locale.fmtInteger = null;
					locale.fmtFloat = null;
					locale.fmtMonth = null;
					locale.fmtYear = null;
				}
			 }
		}
		
		return {
			    getLocaleDatas : getLocaleDatas,
			    getSearchLocaleDatas : getSearchLocaleDatas,
			    setLocale : setLocale
		       };
	}
})(angular);